<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends Public_Controller
{
	function __construct()
	{
		parent::__construct();
		
		$this->load->helper(array('form', 'url', 'date_translator'));
		$this->load->library(array('security', 'pagination', 'form_validation'));
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		
		// load the matching language
		$this->lang->load('pages_blog', $this->imageene->is_language_available($this->session->userdata('uilang')));
	}

	function get_all_categories() {
		$categories = new Blog_category();
		$categories->order_by($this->session->userdata('uilang'))->get();
		
		foreach($categories as $c) {
			$c->posts_in_category = $c->count_posts_in_category($c);
		}
		
		return $categories;
	}
	
	function get_related_posts($post) {
		$related = new Blog_category();
		$related->where('id', $post->category->id)->get();
		$related->posts->where('lang', $this->session->userdata('uilang'))->get();
	
		return $related->posts->all;
	}
	
	function index($post_id = NULL, $category_id = NULL, $page=0)
	{
		// -----------------------------------
		// pagination settings
		// -----------------------------------
		
		$base_url = "/blog/";
		
		if($this->uri->segment(2)=="" || is_numeric($this->uri->segment(2))) {
			$uri_segment = 2;
		}
		
		else {
			$base_url .= $this->uri->segment(2) . "/" . $this->uri->segment(3);
			$uri_segment = 4;
		}
		
		$pagination['base_url'] = $base_url;
		$pagination['per_page'] = $this->config->item('blog_pagination_per_page');
		
		// For fix page numebr
		$pagination['pagination_fix_page_numbers'] = 2;
		
		// The pagination function automatically determines which segment of your URI contains
		// the page number. If you need something different you can specify it.
		//$pagination['uri_segment'] = $this->uri->segment(2) == "liste" ? $this->uri->segment(3) : $this->uri->segment(4);
		$pagination['uri_segment'] = $uri_segment;
		
		// The number of "digit" links you would like before and after the selected page number.
		// For example, the number 2 will place two digits on either side, as in the example links at the very top of this page.
		$pagination['num_links'] = $this->config->item('blog_pagination_num_links');
		
		// By default, the pagination library assume you are using URI Segments, and constructs your
		// links something like http://example.com/index.php/test/page/20
		$pagination['page_query_string'] = FALSE;
		
		$pagination['full_tag_open']	= '<div class="pagination">';
		$pagination['full_tag_close']	= '</div>';
		
		$pagination['first_tag_open']	= '<span class="page">';
		$pagination['first_tag_close']	= '</span>';
		
		$pagination['prev_tag_open']	= '<span class="page">';
		$pagination['prev_tag_close']	= '</span>';
		
		$pagination['cur_tag_open']		= '<span class="page current"><strong>';
		$pagination['cur_tag_close']	= '</strong></span>';
		
		$pagination['next_tag_open']	= '<span class="page">';
		$pagination['next_tag_close']	= '</span>';
		
		$pagination['last_tag_open']	= '<span class="page">';
		$pagination['last_tag_close']	= '</span>';
		
		$pagination['num_tag_open']		= '<span class="page">';
		$pagination['num_tag_close']	= '</span>';
		
		$pagination['first_link']		= 'First';
		$pagination['last_link']		= 'Last';
		$pagination['next_link']		= 'Next';
		$pagination['prev_link']		= 'Previous';
		
		if($category_id != NULL) {
			$pagination['total_rows'] = Blog_post::count_results($category_id, NULL);
		}
		else {
			$pagination['total_rows'] = Blog_post::count_results(NULL, NULL);
		}

		$this->pagination->initialize($pagination);
		
		// -----------------------------------
		// endof: pagination settings
		// -----------------------------------
		
		// get the posts
		if($post_id != NULL) {
			$posts = new Blog_post();
			$posts->limit(1);
			$posts->get_by_id($post_id);
		}
		else if($category_id != NULL) {
			$offset = (int)$this->uri->segment($pagination['uri_segment']);
			
			$c = new Blog_category();
			$c->where('id', $category_id)->get();
			$c->posts->select('id, title, subtitle, text, created, updated, published, deleted');
// 			$c->posts->select('SUBSTRING_INDEX(text, " ", 300) as text', FALSE);
			$c->posts->where('lang', $this->session->userdata('uilang'));
			$c->posts->order_by('created', 'DESC');
			$c->posts->limit($pagination['per_page'], is_int($offset) ? $offset : 0);
			$c->posts->get();
			$posts = $c->posts;
		}
		else {
			$offset = (int)$this->uri->segment($pagination['uri_segment']);
			
			$posts = new Blog_post();
			$posts->select('id, title, subtitle, text, created, updated, published, deleted');
// 			$posts->select('SUBSTRING_INDEX(text, " ", 300) as text', FALSE);
			$posts->where('lang', $this->session->userdata('uilang'));
			$posts->order_by('created', 'DESC');
			$posts->limit($pagination['per_page'], is_int($offset) ? $offset : 0);
			$posts->get();
		}
		
		// get related posts (posts in the same category)
		foreach($posts as $p) {
			$p->related_posts = $this->get_related_posts($p);
		}
		
		$params = array(
			"categories"=> $this->get_all_categories(),
			"posts"		=> $posts,
		);
		
		$this->imageene->page('blog', $params);
	}
	
	function last($page = 0) {
		$this->index(NULL, NULL, $page);		
	}
	
	function post($id = NULL) {
		$this->index(is_numeric((int)$id)?(int)$id:NULL, NULL, NULL);
	}
	
	function category($id = NULL, $page=0) {
		$this->index(NULL, (int)$id, (int)$page);
	}
	
	function search($page=0) {
// 		// -----------------------------------
// 		// pagination settings
// 		// -----------------------------------
		
// 		$pagination['base_url'] = "/blog/search/";
// 		$pagination['per_page'] = $this->config->item('blog_pagination_per_page');
		
// 		// For fix page numebr
// 		$pagination['pagination_fix_page_numbers'] = 2;
		
// 		// The pagination function automatically determines which segment of your URI contains
// 		// the page number. If you need something different you can specify it.
// 		//$pagination['uri_segment'] = $this->uri->segment(2) == "liste" ? $this->uri->segment(3) : $this->uri->segment(4);
// 		$pagination['uri_segment'] = 3;
		
// 		// The number of "digit" links you would like before and after the selected page number.
// 		// For example, the number 2 will place two digits on either side, as in the example links at the very top of this page.
// 		$pagination['num_links'] = $this->config->item('blog_pagination_num_links');
		
// 		// By default, the pagination library assume you are using URI Segments, and constructs your
// 		// links something like http://example.com/index.php/test/page/20
// 		$pagination['page_query_string'] = FALSE;
		
// 		$pagination['full_tag_open']	= '<div class="pagination">';
// 		$pagination['full_tag_close']	= '</div>';
		
// 		$pagination['first_tag_open']	= '<span class="page">';
// 		$pagination['first_tag_close']	= '</span>';
		
// 		$pagination['prev_tag_open']	= '<span class="page">';
// 		$pagination['prev_tag_close']	= '</span>';
		
// 		$pagination['cur_tag_open']		= '<span class="page current"><strong>';
// 		$pagination['cur_tag_close']	= '</strong></span>';
		
// 		$pagination['next_tag_open']	= '<span class="page">';
// 		$pagination['next_tag_close']	= '</span>';
		
// 		$pagination['last_tag_open']	= '<span class="page">';
// 		$pagination['last_tag_close']	= '</span>';
		
// 		$pagination['num_tag_open']		= '<span class="page">';
// 		$pagination['num_tag_close']	= '</span>';
		
// 		$pagination['first_link']		= 'First';
// 		$pagination['last_link']		= 'Last';
// 		$pagination['next_link']		= 'Next';
// 		$pagination['prev_link']		= 'Previous';
		
// 		// -----------------------------------
// 		// endof: pagination settings
// 		// -----------------------------------
		
		$input = $this->input->post('to_search');
		$this->form_validation->set_rules('to_search', 'Search', 'required|trim|xss_clean');
		
		if ($this->form_validation->run()) {
// 			$offset = (int)$this->uri->segment($pagination['uri_segment']);
			
			$posts = new Blog_post();
			
// 			$sql = "SELECT count(id) as total FROM (`blog_posts`) WHERE `blog_posts`.`lang` = ? AND (`blog_posts`.`text` LIKE ? OR `blog_posts`.`title` LIKE ?)";
// 			$res = $this->db->query($sql, array($this->session->userdata('uilang'), "%$input%", "%$input%"));
// 			$pagination['total_rows'] = $res->row('total');
			
			$sql = "SELECT * FROM (`blog_posts`) WHERE `blog_posts`.`lang` = ? AND (`blog_posts`.`text` LIKE ? OR `blog_posts`.`title` LIKE ?) ORDER BY created DESC";
			$posts->query($sql, array($this->session->userdata('uilang'), "%$input%", "%$input%")); 
			
// 			echo "total: " . $pagination['total_rows']."<br>";
// 			echo $this->db->last_query()."<br>";
			
// 			$this->pagination->initialize($pagination);
			
			// get related posts (posts in the same category)
			foreach($posts as $p) {
				$p->related_posts = $this->get_related_posts($p);
			}
			
			$params = array(
						"categories"=> $this->get_all_categories(),
						"posts"		=> $posts,
			);
			
			$this->imageene->page('blog', $params);
		}

		else {
			redirect('/blog');
		}
	}
}

/* End of file pages.php */
/* Location: ./application/controllers/pages.php */